package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("插入操作！");
        metaObject.setValue("createTime", LocalDateTime.now()); // 当前的时间
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());  // 目前是死数据
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("更新操作！");
//        System.out.println(metaObject.toString());
        metaObject.setValue("updateTime",LocalDateTime.now()); // 更新的时候，更新的时间
        metaObject.setValue("updateUser",BaseContext.getCurrentId()); // 更新用户的时候，更新用户id
    }
}
